/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pushy.client;

import java.awt.Dimension;

/**
 *
 * @author philipp
 */
public class Challenge extends javax.swing.JPanel {

    Client client;
    
    /**
     * Creates new form Challenge
     */
    public Challenge(Client client) {
        initComponents();
        this.client = client;
        setSize(500, 400);
        setPreferredSize(new Dimension(500, 400));
        setVisible(true);
        refresh();
        
        client.getGUI().setTitle("Pushynator - Spieler herausfordern");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        list = new javax.swing.JTextArea();
        name = new javax.swing.JTextField();
        challenge = new javax.swing.JButton();
        refresh = new javax.swing.JButton();
        back = new javax.swing.JButton();
        notification = new javax.swing.JLabel();

        jScrollPane1.setEnabled(false);

        list.setColumns(20);
        list.setRows(5);
        jScrollPane1.setViewportView(list);

        challenge.setText("Einladen");
        challenge.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                challengeActionPerformed(evt);
            }
        });

        refresh.setText("Aktualisieren");
        refresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                refreshActionPerformed(evt);
            }
        });

        back.setText("Zurück");
        back.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backActionPerformed(evt);
            }
        });

        notification.setMaximumSize(new java.awt.Dimension(200, 176));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(challenge, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(refresh, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE)
                    .addComponent(name)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(notification, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(61, 61, 61)
                        .addComponent(back, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(challenge)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(refresh)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(notification, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
                                .addGap(35, 35, 35))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(back)
                                .addGap(0, 0, Short.MAX_VALUE))))
                    .addComponent(jScrollPane1))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void backActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backActionPerformed
        client.getGUI().MainMenu();
    }//GEN-LAST:event_backActionPerformed

    private void refreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshActionPerformed
        refresh();
    }//GEN-LAST:event_refreshActionPerformed

    private void challengeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_challengeActionPerformed
        if(name.getText().length() == 0) {
            notification.setText("Name darf nicht leer sein");
        }else {
            notification.setText("Herausforderung wird an " + name.getText() + " gesendet");
            challenge.setEnabled(false);
            back.setEnabled(false);
            client.challenge(name.getText());
        }
    }//GEN-LAST:event_challengeActionPerformed

    private void refresh() {
        list.setText(client.onlinelist());
    }
    
    public void challengeResponse(boolean accepted) {
        if(accepted) {
            notification.setText("Herausforderung angenommen");
            client.getGUI().Levelansicht();
        }else {
            notification.setText("Herausforderung abgelehnt");
        }
        challenge.setEnabled(true);
        back.setEnabled(true);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton back;
    private javax.swing.JButton challenge;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea list;
    private javax.swing.JTextField name;
    private javax.swing.JLabel notification;
    private javax.swing.JButton refresh;
    // End of variables declaration//GEN-END:variables
}
